<?php

namespace App\Listeners;

use App\Events\ExampleEvent;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\Log;

class ExampleListener implements ShouldQueue
{
    /**
     * 任务将被发送到的连接的名称
     *
     * @var string|null
     */
    public $connection = 'redis';

    /**
     * 任务将被发送到的队列的名称
     *
     * @var $queue string|null
     */
    public $queue = 'ExampleListener{queue}';

    /**
     * 任务被处理的延迟时间（秒）
     *
     * @var $delay int
     */
    public $delay = 0;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param ExampleEvent $event
     * @return void
     */
    public function handle(ExampleEvent $event)
    {
//        User::query()->where('a',1)->first();
        sleep(5);
        Log::info(json_encode($event->data,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 确定监听器是否应加入队列
     *
     * @param ExampleEvent $event
     * @return bool
     */
    public function shouldQueue(ExampleEvent $event): bool
    {
        return true;
    }

    /**
     * @param ExampleEvent $event
     * @param $exception
     * @return void
     */
    public function failed(ExampleEvent $event, $exception)
    {
        Log::info('this is failed listeners queue of exampleListener '.json_encode($event->data,JSON_UNESCAPED_UNICODE));
    }

}
